//
//  FDMKindCollectionViewCell.m
//  Entertainment
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMKindCollectionViewCell.h"
#import "FDMKindDetaileCollectionViewCell.h"
#import "FDMFindDetaileModel.h"
#import "FDMKindDetaileReusableView.h"


@interface FDMKindCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) FDMBaseCollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSourceArray;

@end
@implementation FDMKindCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        /** 创建集合视图 */
        [self createCollectionView];
        
        /** 数据处理 */
        [self dataHandel];
        
        self.dataSourceArray = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc {
    [self.collectionView release];
    [self.dataSourceArray release];
    [super dealloc];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 2, 40);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[FDMBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 153) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.80 green:0.80 blue:0.80 alpha:1.00];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView registerClass:[FDMKindDetaileCollectionViewCell class] forCellWithReuseIdentifier:@"FDMKindDetaileCollectionViewCell"];
    [self.collectionView registerClass:[FDMKindDetaileReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FDMKindDetaileReusableView"];
}

#pragma mark - collectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SCREEN_WIDTH, 150);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSourceArray.count - 5;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        
        if (indexPath.row < 5) {
            FDMKindDetaileReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"FDMKindDetaileReusableView" forIndexPath:indexPath];
            if (self.dataSourceArray.count) {
                header.modelArr = self.dataSourceArray;
            }
            return header;
        }
    }
    return nil;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FDMKindDetaileCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FDMKindDetaileCollectionViewCell" forIndexPath:indexPath];
    FDMFindDetaileModel *model = self.dataSourceArray[indexPath.row + 5];
    cell.findDetaileModel = model;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (void)dataHandel {
    NSString *urlStr = @"http://mobile.ximalaya.com/mobile/discovery/v1/categories?channel=and-f6&device=android&picVersion=11&scale=2&statEvent=pageview/tab@发现_分类";
    [APPTools GETWithURL:urlStr par:nil success:^(id responseObject) {
        
        self.dataSourceArray = [FDMFindDetaileModel baseModelHandelWithArray:responseObject[@"list"]];
        
        [self.collectionView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
